#include "LCD_DISPLAY.h"

volatile    unsigned char COM_STATE;
volatile    unsigned char DISPLAY_RAM0;
volatile    unsigned char DISPLAY_RAM1;
volatile    unsigned char DISPLAY_RAM2;
volatile    unsigned char DISPLAY_RAM3;
//                                  DISPLAY_RAM0            DISPLAY_RAM1            DISPLAY_RAM2            DISPLAY_RAM3
//            SCAN_RAMBIT       S_SEG0  /   S_SEG1      S_SEG2  /   S_SEG3      S_SEG4  /   S_SEG5      S_SEG6  /   S_SEG7
//S_COM0      1                 bit0    /   bit4        bit0    /   bit4        bit0    /   bit4        bit0    /   bit4
//S_COM1      2                 bit1    /   bit5        bit1    /   bit5        bit1    /   bit5        bit1    /   bit5
//S_COM2      4                 bit2    /   bit6        bit2    /   bit6        bit2    /   bit6        bit2    /   bit6
//S_COM3      8                 bit3    /   bit7        bit3    /   bit7        bit3    /   bit7        bit3    /   bit7

void LCD_initial()
{
    _pas17 = 1; _pas16 = 0;     //  PA7     COM7/SEG7
    _pas15 = 0; _pas14 = 1;     //  PA6     COM6/SEG6
    _pas13 = 0; _pas12 = 1;     //  PA5     COM5/SEG5
    _pas11 = 1; _pas10 = 0;     //  PA4     COM4/SEG4
    _pac = 0x0f;

    _pbs15 = 1; _pbs14 = 1;     //  PB6     COM/SEG14
    _pbs13 = 1; _pbs12 = 1;     //  PB5     COM/SEG13
    _pbs11 = 1; _pbs10 = 0;     //  PB4     COM/SEG12
    _pbs07 = 1; _pbs06 = 0;     //  PB3     COM/SEG11
    _pbc = 0x87;

    _pds07 = 1; _pds06 = 0;     //  PD3     SSEG25
    _pds05 = 1; _pds04 = 0;     //  PD2     SSEG24
    _pds03 = 1; _pds02 = 0;     //  PD1     SSEG23
    _pds01 = 1; _pds00 = 1;     //  PD0     SSEG22
    _pdc = 0xf0;

    //LCD配置
    _slcdc0 =   0b01010000;
    _slcds0 =   0b00000000;     //COM
    _slcds1 =   0b01111000;     //SEG
    _slcds2 =   0b00000000;

    //TM配置
    _ctmc0 =    0b00110000;     //fsys/64
    _ctmc1 =    0b11000001;     //定时 / 计数器模式;    CTM 比较器 A 匹配
    _ctmal =    (SYS_FQC/64/LCD_FQC/COM_NUM/2)&0xff;
    _ctmah =    (SYS_FQC/64/LCD_FQC/COM_NUM/2)>>8;
    _ctmrp =    0;

    //中斷配置
    _mf1f =     0;
    _mf1e =     1;
    _ctmaf =    0;
    _ctmae =    1;
    COM_STATE = 0;
    _cton = 1;
    
    DISPLAY_RAM0 = 0b11001100;
    DISPLAY_RAM1 = 0b00110011;
    DISPLAY_RAM2 = 0b10101010;
    DISPLAY_RAM3 = 0b01010101;
}

void __attribute((interrupt(0x10))) ISR_CTM_PTM(void)
{
    unsigned char SCAN_RAMBIT;
    unsigned char DISPLAY_TEMP;
    if(_ctmaf)
    {
        _ctmaf = 0;
	    COM_STATE++;
	    if(COM_STATE==(COM_NUM*2))
	    {
	        COM_STATE = 0;
	    }
	    _frame ^= 1;
	    switch(COM_STATE/2)
	    {
	        #ifdef  S_COM0
	        case 0:      SCAN_RAMBIT = 0b00000001;goto  SCOM0_Work;break;
	        #endif
	        #ifdef  S_COM1
	        case 1:      SCAN_RAMBIT = 0b00000010;goto  SCOM1_Work;break;
	        #endif
	        #ifdef  S_COM2
	        case 2:      SCAN_RAMBIT = 0b00000100;goto  SCOM2_Work;break;
	        #endif
	        #ifdef  S_COM3
	        case 3:      SCAN_RAMBIT = 0b00001000;goto  SCOM3_Work;break;
	        #endif
	
	        default:     SCAN_RAMBIT = 0b00000001;COM_STATE = 0;_frame = 0;break;
	    }

SCOM0_Work:
	    S_COM0 = 1;
	    S_COM1 = 0;
	    S_COM2 = 0;
	    S_COM3 = 0;
	    goto    SSEG_WORK;

SCOM1_Work:
	    S_COM0 = 0;
	    S_COM1 = 1;
	    S_COM2 = 0;
	    S_COM3 = 0;
	    goto    SSEG_WORK;

SCOM2_Work:
	    S_COM0 = 0;
	    S_COM1 = 0;
	    S_COM2 = 1;
	    S_COM3 = 0;
	    goto    SSEG_WORK;

SCOM3_Work:
	    S_COM0 = 0;
	    S_COM1 = 0;
	    S_COM2 = 0;
	    S_COM3 = 1;
	//    goto    SSEG_WORK;



SSEG_WORK:
	    DISPLAY_TEMP = DISPLAY_RAM0;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG0 = 0;
	    }else   S_SEG0 = 1;
	    DISPLAY_TEMP>>=4;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG1 = 0;
	    }else   S_SEG1 = 1;
	
	    DISPLAY_TEMP = DISPLAY_RAM1;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG2 = 0;
	    }else   S_SEG2 = 1;
	    DISPLAY_TEMP>>=4;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG3 = 0;
	    }else   S_SEG3 = 1;
	
	    DISPLAY_TEMP = DISPLAY_RAM2;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG4 = 0;
	    }else   S_SEG4 = 1;
	    DISPLAY_TEMP>>=4;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG5 = 0;
	    }else   S_SEG5 = 1;
	
	    DISPLAY_TEMP = DISPLAY_RAM3;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG6 = 0;
	    }else   S_SEG6 = 1;
	    DISPLAY_TEMP>>=4;
	    if((DISPLAY_TEMP&SCAN_RAMBIT) == 0)
	    {
	        S_SEG7 = 0;
	    }else   S_SEG7 = 1;
    }
}
